package my.edu.model.bills.purchase.controller;

import cn.hutool.json.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.groups.Default;
import my.edu.aspect.LogExecutionTime;
import my.edu.common.domain.PageVO;
import my.edu.common.validate.groups.Insert;
import my.edu.common.domain.IdsRequest;
import my.edu.common.domain.PageDTO;
import my.edu.common.domain.Response;
import my.edu.common.validate.groups.Purchase;
import my.edu.common.validate.groups.Sales;
import my.edu.constant.DeliveryMethod;
import my.edu.model.bills.purchase.entity.PurchaseOrder;
import my.edu.model.bills.purchase.entity.dto.PurchaseDTO;
import my.edu.model.bills.purchase.entity.dto.PurchaseOrderSearchDTO;
import my.edu.model.bills.purchase.entity.dto.PurchaseReturnOrderSearchDTO;
import my.edu.model.bills.purchase.service.IPurchaseOrderService;
import my.edu.utils.StringUtils;
import my.edu.websocket.BillWebSocket;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/purchaseOrder")
@Tag(name = "采购订单管理")
@Validated
public class PurchaseOrderController {
    @Autowired
    private IPurchaseOrderService purchaseOrderService;
    @Autowired
    private DeliveryMethod deliveryMethod;


    @PutMapping
    @RequiresPermissions("purchase:save")
    @Operation(summary = "新增订单")
    public Response addPurchaseOrder(@RequestBody @Validated(value = {Insert.class}) PurchaseDTO purchaseDTO, @RequestParam(required = false) String status) {
        purchaseOrderService.saveNew(purchaseDTO, status);
        return Response.success();
    }

    @DeleteMapping("/deleteBatch")
    @RequiresPermissions("purchase:del")
    @Operation(summary = "批量删除订单")
    public Response delPurchaseOrder(String ids) {
        List<Long> longs = StringUtils.strToLongList(ids);
        purchaseOrderService.removeOrder(longs);
        return Response.success();
    }

    @DeleteMapping("/delete")
    @RequiresPermissions("purchase:del")
    @Operation(summary = "删除订单")
    public Response delete(Long id) {
        purchaseOrderService.removeOrder(List.of(id));
        return Response.success();
    }

    @PostMapping("/update")
    @RequiresPermissions("purchase:update")
    @Operation(summary = "修改订单")
    public Response updatePurchaseOrder(@RequestBody @Validated PurchaseDTO purchaseDTO, @RequestParam(required = false) String status) {
        purchaseOrderService.updateOrder(purchaseDTO, status);
        return Response.success();
    }

    @PostMapping("/batchSetStatus")
    @RequiresPermissions(value = {"purchase:audit"})
    @Operation(summary = "更新审核状态")
    public Response batchSetStatus(@RequestBody JSONObject jsonObject) {
        int status = jsonObject.getInt("status");
        String ids = jsonObject.getStr("ids");
        List<Long> longs = StringUtils.strToLongList(ids);
        if (status == 1) {
            purchaseOrderService.auditOrder(longs);
        } else if (status == 0) {
            purchaseOrderService.rauditOrder(longs);
        } else {
            return Response.error("状态值错误");
        }
        return Response.success();
    }


    @GetMapping("/list")
    @RequiresPermissions("purchase:get")
    @Operation(summary = "查询订单")
    public Response getPurchaseOrder(@ModelAttribute @Validated PageDTO page,
                                     @ModelAttribute PurchaseOrderSearchDTO purchaseOrderSearchDTO) {
        return Response.success(purchaseOrderService.getPurchaseOrderPage(page, purchaseOrderSearchDTO));
    }

    @GetMapping("/one")
    @RequiresPermissions("purchase:get")
    @Operation(summary = "根据id查询查询订单")
    public Response getPurchaseOrder(@NotNull(message = "id不能为空") Long id) {
        return Response.success(purchaseOrderService.getOrderOne(id));
    }

    @GetMapping("/deliveryMethod")
    @Operation(summary = "获取交货方式")
    public Response getPurchaseOrderOne() {
        return Response.success(deliveryMethod.getDeliveryMethod());
    }

    @GetMapping
    @Operation(summary = "查询全部订单")
    public Response getPurchaseReturnrderOnly(@ModelAttribute PurchaseOrderSearchDTO purchaseOrderSearchDTO) {
        return Response.success(purchaseOrderService.listOnly(purchaseOrderSearchDTO));
    }

    @LogExecutionTime
    @GetMapping("/buyIn")
    @RequiresPermissions("purchase:get")
    @Operation(summary = "查询报表")
    public Response getPurchaseReport(@ModelAttribute PurchaseOrderSearchDTO purchaseOrderSearchDTO,
                                      @ModelAttribute PageDTO pageDTO) {
        return Response.success(purchaseOrderService.getPurchaseReportList(pageDTO,purchaseOrderSearchDTO));
    }
}